<template>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column label="用户名">
      <template slot-scope="scope">
        <el-popover trigger="hover" placement="top">
          <p>用户名: {{ scope.row.username }}</p>
          <p>id: {{ scope.row.id }}</p>
          <div slot="reference" class="name-wrapper">
            <el-tag size="medium">{{ scope.row.username }}</el-tag>
          </div>
        </el-popover>
      </template>
    </el-table-column>
    <el-table-column label="学号">
      <template slot-scope="scope">
        <span>{{ scope.row.number }}</span>
      </template>
    </el-table-column>
    <el-table-column label="真实姓名">
      <template slot-scope="scope">
        <span style="margin-left: 10px">{{ scope.row.fullname }}</span>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button
          @click="open(scope.$index, scope.row)"
          type="danger"
          plain
          size="mini"
          class="delBtn"
        >删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
    };
  },
  created() {
    this.getInfo();
  },
  methods: {
    // 删除弹框
    open(index, row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 确定删除
          this.$axios({
            url: "/api/deleteUser",
            method: "post",
            data: {
              id: String(row.id),
            },
          }).then((res) => {
            if (res._RejCode === "00000000") {
              this.$message({
                message: "删除成功",
                type: "success",
              });
              this.getInfo();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);

      this.$axios({
        url: "/api/deleteUser",
        method: "post",
        data: {
          id: String(row.id),
        },
      }).then((res) => {
        if (res._RejCode === "00000000") {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getInfo();
        }
      });
    },
    // 获取教学楼
    getInfo() {
      this.$axios({
        url: "/api/userQ",
        method: "post",
        data: {},
      }).then((res) => {
        if (res._RejCode === "00000000") {
          this.tableData = res.users;
        }
      });
    },
  },
};
</script>